package com.linkgie.galaxyframework.utils;

public class VersionUtils {

	/**
	 * 把版本号编码为 32 位正整数；
	 * 
	 * @param major 主版本号，合法的取值区间 [0 ~ 127]；
	 * @param minor 次版本号，合法的取值区间 [0 ~ 127]；
	 * @param build 修正版本号，合法的取值区间 [0 ~ 127]；
	 * @return
	 */
	public static int encodeVersion(byte major, byte minor, byte build) {
		if (major < 0) {
			throw new IllegalArgumentException("The value of major is negative!");
		}
		if (minor < 0) {
			throw new IllegalArgumentException("The value of minor is negative!");
		}
		if (build < 0) {
			throw new IllegalArgumentException("The value of build is negative!");
		}

		return BinaryUtils.toInt(new byte[] { 0, major, minor, build });
	}

	public static String formatVersion(int versionCode) {
		byte[] sections = BinaryUtils.toBytes(versionCode);
		return formatVersion(sections[1], sections[2], sections[3], "");
	}
	
	public static String formatVersion(int versionCode, String tag) {
		byte[] sections = BinaryUtils.toBytes(versionCode);
		return formatVersion(sections[1], sections[2], sections[3], tag);
	}

	public static String formatVersion(byte major, byte minor, byte build, String tag) {
		return String.format("%s.%s.%s%s", major, minor, build, tag);
	}

}
